package boot3.service.impl;

import boot3.mapper.StudentMapper;
import boot3.mapper.UserMapper;
import boot3.pojo.Student;
import boot3.pojo.User;
import boot3.service.StudentService;
import boot3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public Student getStudentById(String student_id) {
        return studentMapper.getStudentById(student_id);
    }

    @Override
    public Boolean updateStudent(String student_id, String item, String value) {
        return studentMapper.updateStudent(student_id, item, value);
    }

    @Override
    public List<Student> getStudentByCollege(String college) {
        return studentMapper.getStudentByCollege(college);
    }

    @Override
    public List<String> findCollege() {
        return studentMapper.findCollege();
    }

    @Override
    public Boolean addStudent(Student student) {
       //获取当前所有的用户信息
        List<User> allUsers= userMapper.findAll();
        String lastID=allUsers.get(allUsers.size()-1).getUserID();
        String newUserID= String.valueOf(Long.parseLong(lastID)+1);
        student.setStudent_id(allUsers.isEmpty()?"202011070100":newUserID);
        User user=new User();
        user.setUserID(student.getStudent_id());
        user.setUserName(student.getStudent_name());
        user.setType("stu");
        user.setPassword("123123");
       return  userMapper.addUser(user) && studentMapper.addStudent(student);
    }

    @Override
    public Boolean deleteStudent(String student_id) {
        return userMapper.delUser(student_id) && studentMapper.deleteStudent(student_id);
    }
}
